﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OutSourceingServices.Models.DataAccess;

namespace OutSourceingServices.Models
{
    public enum CommunityType
    {
        DESKTOP,
        WEB,
        MOBILE,
        INVALID = Int32.MaxValue
    }

    public class Community
    {
        public String ID { get; set; }
        public String Title { get; set; }
        public String Text { get; set; }
        public DateTime CreatedDate { get; set; }
        public CommunityType Type { get; set; }
    }

    public class CommunityDBContext
    {
        public const Int16 NUMBER_OF_HOT_NEWS = 3;
        private IDataProvider dbProvider;

        // Destop articles
        public List<Community> DesktopArticles
        {
            get
            {
                List<Community> data = dbProvider.LoadCommunityContent();
                // Create Desktop article list
                return (from item in data
                        where item.Type == CommunityType.DESKTOP
                        orderby item.CreatedDate
                        select item).ToList<Community>();
            }
        }
        // Web articles
        public List<Community> WebArticles
        {
            get
            {
                List<Community> data = dbProvider.LoadCommunityContent();
                // Create Desktop article list
                return (from item in data
                        where item.Type == CommunityType.WEB
                        orderby item.CreatedDate
                        select item).ToList<Community>();
            }
        }
        // Mobile articles
        public List<Community> MobileArticles
        {
            get
            {
                List<Community> data = dbProvider.LoadCommunityContent();
                // Create Desktop article list
                return (from item in data
                        where item.Type == CommunityType.MOBILE
                        orderby item.CreatedDate
                        select item).ToList<Community>();
            }
        }
        // All articles
        public List<Community> Articles
        {
            get
            {
                List<Community> data = dbProvider.LoadCommunityContent();
                // Create Desktop article list
                return (from item in data
                        orderby item.CreatedDate
                        select item).ToList<Community>();
            }
        }

        // All articles
        public List<Community> NewestArticles
        {
            get
            {
                List<Community> lstResult = new List<Community>();
                Int16 iCounter = 0;
                foreach (Community item in this.Articles)
                {
                    // add to result list
                    lstResult.Add(item);
                    // increase counter
                    iCounter++;
                    // break if max item reached
                    if (iCounter == NUMBER_OF_HOT_NEWS)
                    {
                        break;
                    }
                }

                return lstResult;
            }
        }

        /// <summary>
        /// Create an instance of CommunityDBContext.
        /// </summary>
        public CommunityDBContext()
        {
            dbProvider = DataProvider.GetDataProvider();
        }

        /// <summary>
        /// Find article by ID.
        /// </summary>
        /// <param name="articleID">ID of the article.</param>
        /// <returns>The article was found. "Null" if it does not exist.</returns>
        public Community FindById(string articleID)
        {
            return (from item in this.Articles
                    where item.ID == articleID
                    select item).FirstOrDefault();
        }
    }
}